package org.miyuki4j.cfgtool.app.gui;

import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

@Slf4j
class SceneLoader {
    static String getFxmlName(String fxmlName) {
        if (!fxmlName.endsWith(".fxml")) {
            fxmlName += ".fxml";
        }
        return "/gui/"+ fxmlName;
    }

    static void load(String fxmlName) {
        Parent root = loadParent(fxmlName);
        if (root != null) {
            try {
                MainGui.setParent(root);
            } catch (Exception e) {
                log.error("set parent error|fxmlName={}", fxmlName, e);
            }
        }
    }

    static Parent loadParent(String fxmlName) {
        try {
            FXMLLoader loader = new FXMLLoader(SceneLoader.class.getResource(getFxmlName(fxmlName)));
            return loader.load();
        } catch (Exception e) {
            log.error("load parent fxml error|fxmlName={}", fxmlName, e);
            return null;
        }
    }
}